from django.db import models
from user.models import User


class Questionnaire(models.Model):
    title = models.CharField(max_length=50, verbose_name='问卷标题')
    username = models.CharField(max_length=20, verbose_name='发起人')  # 关联用户名
    # user = models.ForeignKey(User, verbose_name='发起人', on_delete=models.CASCADE)
    status_type = ((0, u'未发布'), (1, u'已发布'))
    status = models.IntegerField(choices=status_type, verbose_name='是否发布', default=0)  # 0未发布 1已发布
    desc = models.TextField(verbose_name='问卷说明', null=True)  # 问卷描述 在问卷头部展示
    created_time = models.DateTimeField(auto_now_add=True)  # 创建时间
    publish_time = models.DateTimeField(null=True, default=None)  # 发布时间
    start_time = models.DateTimeField(null=True, default=None)  # 开始时间
    end_time = models.DateTimeField(null=True, default=None)  # 截止时间
    is_remind = models.BooleanField(null=True, default=False)  # 是否设置提醒
    is_allowed_to_change = models.BooleanField(null=True, default=False)  # 是否允许用户修改
    is_out_of_order = models.BooleanField(null=True, default=False)  # 是否乱序
    show_number = models.BooleanField(null=True, default=False)  # 是否显示题号
    is_deleted = models.BooleanField(null=True, default=False)  # 是否被回收
    is_set_date = models.BooleanField(null=True, default=False)  # 是否设置起始时间
    type = models.CharField(max_length=15, default='default')  # 问卷类型：default vote apply exam
    limit = models.IntegerField(null=True, default=None)  # 选项限制人数

    class Meta:
        verbose_name = u'问卷'
        verbose_name_plural = u'问卷列表'


class Question(models.Model):
    title = models.CharField(max_length=100, verbose_name='题目标题')
    type = models.CharField(max_length=20, verbose_name='题目类型')
    wjId = models.IntegerField(verbose_name='关联问卷id')
    # questionnaire = models.ForeignKey(Questionnaire, verbose_name='关联问卷id')
    row = models.IntegerField(verbose_name='行数', null=True)  # 如果为填空题 此字段为文本输入框的行数
    must = models.BooleanField(verbose_name='是否必填')  # 是否必填
    remark = models.CharField(max_length=100, default="")  # 题目备注
    next = models.IntegerField(default=-1)  # 题目的后继 -1表示没有后继
    score = models.IntegerField(null=True, default=None)  # 题目的分值
    correct_option = models.IntegerField(null=True, default=None)  # 正确的选项
    correct_answer = models.CharField(max_length=100, null=True, default=None)  # 正确的答案（填空题
    is_positioning = models.BooleanField(default=False)  # 是否为定位题



class Options(models.Model):
    questionId = models.IntegerField(verbose_name='关联题目id')
    # question = models.ForeignKey(Question, verbose_name='关联题目id')
    title = models.CharField(max_length=100, verbose_name='选项名')
    limit = models.IntegerField(null=True, default=None)  # 选项限制人数
    remain = models.IntegerField(null=True, default=None)  # 选项剩余数量
    is_correct = models.BooleanField(default=False)  # 是否为正确选项


# 提交信息表
class Submit(models.Model):
    wjId = models.IntegerField(verbose_name='关联问卷id')
    # questionnaire = models.ForeignKey(Questionnaire, verbose_name='关联问卷id')
    submitTime = models.DateTimeField(verbose_name='提交时间', null=True, default=None)
    submitIp = models.CharField(max_length=15, verbose_name='提交ip', null=True, default=None)
    useTime = models.IntegerField(verbose_name='填写用时', null=True, default=None)  # 单位：秒
    is_submitted = models.BooleanField(null=True, default=False)  # 是否已经被提交
    username = models.CharField(max_length=50, null=True, default=None)  # 或有或无的用户名


class Answer(models.Model):
    questionId = models.IntegerField(verbose_name='关联问题id')
    # question = models.ForeignKey(Question, verbose_name='关联题目id')
    submitId = models.IntegerField(verbose_name='关联提交id')
    # submit = models.ForeignKey(Submit, verbose_name='关联提交id')
    wjId = models.IntegerField(verbose_name='问卷id')
    type = models.CharField(max_length=20, verbose_name='题目类型')
    answer = models.IntegerField(verbose_name='答案', blank=True, null=True, default=None)
    answerText = models.TextField(verbose_name='文本答案', blank=True, null=True, default=None)


class GradeQuestionAttribute(models.Model):
    questionId = models.IntegerField()
    grade_type = models.IntegerField()  # 1：满意度  2：认同度  3：重要度  4： 自定义
    degree_min = models.CharField(max_length=20)
    degree_max = models.CharField(max_length=20)
    range = models.IntegerField()
    start = models.IntegerField()


class Relevancy(models.Model):
    optionId = models.IntegerField()
    questionId = models.IntegerField()
